/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：MemberDataItemContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseInfoItem;
import com.xteam.ecsystem.service.admin.MemberDataItemService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 会员资料项控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class MemberDataItemContorller {

	@Autowired
	private MemberDataItemService mdiService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getMdiList.do")
	public void getMdiList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		String strList = this.mdiService.getMdiList(pageIndex, pageSize);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateMdi.do")
	public void saveOrUpdateMdi(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "memberDataItem");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);		
		BaseInfoItem mdi = (BaseInfoItem)JSONObject.toBean(obj, BaseInfoItem.class);
		
		if(StringUtil.isNotNullOrEmpty(mdi.getBiiId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERITEM_MGR, 
				"saveOrUpdateObject", lslType, "BaseInfoItem");
		
		this.mdiService.saveOrUpdateObject(mdi, mdi.getBiiId());
	}
	
	@RequestMapping("/getMdiById.do")
	public void getBaiById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String biiId = ReqHelper.getRequestString(request, "biiId");		
		String strInfo = this.mdiService.getMdiById(biiId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateMdiStateById.do")
	public void updateMdiStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String biiId = ReqHelper.getRequestString(request, "biiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.mdiService.updateMdiStateById(biiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERITEM_MGR, 
				"updateMdiStateById", ConstantLocation.LOG_Edit_STATE, "BaseInfoItem");

	}
	
	@RequestMapping("/updateRegStateById.do")
	public void updateRegStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String biiId = ReqHelper.getRequestString(request, "biiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.mdiService.updateRegStateById(biiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERITEM_MGR, 
				"updateRegStateById", ConstantLocation.LOG_Edit_STATE, "BaseInfoItem");

	}
	
	@RequestMapping("/delMdiByIds.do")
	public void delMdiByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String biiIds = ReqHelper.getRequestString(request, "biiIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.mdiService.delObject(biiIds);

		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_MEMBERITEM_MGR, 
				"delObject", ConstantLocation.LOG_DELETE, "BaseInfoItem");
	}
	
	@RequestMapping("/getBmiBybii.do")
	public void getBmiBybii(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String biiIds = ReqHelper.getRequestString(request, "biiIds");

		boolean isUsed = this.mdiService.getBmiBybii(biiIds);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
